<?php
/**
 * 收货地址
 * 2019.3.11
 * xuxh
 */
namespace app\api\controller;

use app\api\validate\AddressValidate;
use think\Db;

class AddressController extends BaseController
{
	/**
	 * 首页接口
	 */
	public function index()
	{
	    $this->checkUser();
	    $user_id = $this->user_id;
	    $addressLists = Db::name('user_express_address')->alias('a')
            ->field('a.*,a.province as provinceid,a.city as cityid,a.district as districtid,r.name as province,e.name as city,g.name as district')
            ->join('region r','r.id = a.province')
            ->join('region e','e.id = a.city')
            ->join('region g','g.id = a.district')
            ->where('a.user_id',$user_id)
            ->select();
	    $addressData = array();
        foreach ($addressLists as $address) {
            $info = array('addr_id'=>$address['id'],'consigner'=>$address['consigner'],'mobile'=>$address['mobile'],'provinceid'=>$address['provinceid'],'cityid'=>$address['cityid'],'districtid'=>$address['districtid'],'province'=>$address['province'],'city'=>$address['city'],'district'=>$address['district'],'address'=>$address['address']);
            $addressData[] = $info;
	    }
        $this->ApiResult(true,10000,'操作成功',$addressData);
	}

    /**
     * 添加收货地址
     */
	function addPost(){
        $this->checkUser();
        $user_id = $this->user_id;
        $post = $this->request->post();
        $addressValid = new AddressValidate();
        $result = $addressValid->check($post);
        if(!$result){
            $this->ApiResult(false,10001,$addressValid->getError());
        }
        $post['user_id'] = $user_id;
        //是否有默认地址
        $defaultData = Db::name('user_express_address')->where('user_id = "'.$user_id.'" and is_default = 1')->find();
        if(!$defaultData){
            $post['is_default'] = 1;
        }
        $rs = Db::name('user_express_address')->insert($post);
        if($rs){
            $this->ApiResult(true,10000,'添加成功');
        }else{
            $this->ApiResult(false,10001,'添加失败');
        }
    }

    /**
     * 批量删除收货地址
     */
    function delBatch(){
        $this->checkUser();
        $user_id = $this->user_id;
        $ids = $this->request->post('ids');
        $ids = trim($ids,',');
        if(!$ids){
            $this->ApiResult(false,10001,'参数传入错误');
        }
        $rs = Db::name('user_express_address')->whereIn('id',$ids)->delete();
        if($rs){
            $this->ApiResult(true,10000,'删除成功');
        }else{
            $this->ApiResult(false,10001,'删除失败');
        }
    }

    /**
     * 提交修改
     */
    function editPost(){
        $this->checkUser();
        $post = $this->request->post();
        if(!$post['id']){
            $this->ApiResult(false,10001,'参数传入错误');
        }
        $addressValid = new AddressValidate();
        $result = $addressValid->check($post);
        if(!$result){
            $this->ApiResult(false,10001,$addressValid->getError());
        }
        $rs = Db::name('user_express_address')->where('id',$post['id'])->update($post);
        if($rs){
            $this->ApiResult(true,10000,'修改成功');
        }else{
            $this->ApiResult(false,10001,'修改失败');
        }
    }

}